package com.application.merecipe.client;

import java.util.Iterator;

import com.application.merecipe.shared.Recipe;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class RecipeComposite extends Composite {
	private Recipe recipe = new Recipe();
	private DecoratorPanel decoratorPanel = new DecoratorPanel();
	private TextArea textAreaDescription = new TextArea();
	private TextArea textAreaIngredients = new TextArea();
	private TextBox textBoxKeywords = new TextBox();
	private TextBox textBoxNoPersons = new TextBox();
	private TextBox textBoxTitle = new TextBox();
	private RecipeServiceAsync asyncService = GWT.create(RecipeService.class);
	private DialogBox dialogbox;

	public RecipeComposite(Recipe recipe) {
		this.setRecipe(recipe);
		VerticalPanel verticalPanel = new VerticalPanel();
		decoratorPanel.setWidget(verticalPanel);
		verticalPanel.setSize("1000px", "500px");

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setSize("1000px", "30px");

		Button btnSave = new Button("Save");
		horizontalPanel.add(btnSave);
		btnSave.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveRecipe();
			}

		});
		Button btnDelete = new Button("Delete");
		btnDelete.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				showAreYouSure();
			}

		});
		horizontalPanel.add(btnDelete);

		HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_5);
		horizontalPanel_5.setSize("1000px", "30px");

		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_5.add(horizontalPanel_1);
		horizontalPanel_1.setSize("350px", "30px");

		Label lblTitle = new Label("Title:");
		horizontalPanel_1.add(lblTitle);

		horizontalPanel_1.add(textBoxTitle);
		textBoxTitle.setWidth("300px");

		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		horizontalPanel_5.add(horizontalPanel_2);
		horizontalPanel_2.setSize("250px", "30px");

		Label lblNewLabel = new Label("Keywords:   ");
		horizontalPanel_2.add(lblNewLabel);

		textBoxKeywords.setText("");
		horizontalPanel_2.add(textBoxKeywords);

		HorizontalPanel horizontalPanel_7 = new HorizontalPanel();
		horizontalPanel_5.add(horizontalPanel_7);
		horizontalPanel_7.setSize("250px", "30px");

		Label lblNewLabel_1 = new Label("No of Persons:");
		horizontalPanel_7.add(lblNewLabel_1);

		horizontalPanel_7.add(textBoxNoPersons);
		textBoxNoPersons.setText("");

		HorizontalPanel horizontalPanel_6 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_6);
		horizontalPanel_6.setSize("1000px", "400px");

		HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		horizontalPanel_6.add(horizontalPanel_3);
		horizontalPanel_3.setSize("500px", "400px");

		Label lblNewLabel_2 = new Label("Ingredients:");
		horizontalPanel_3.add(lblNewLabel_2);

		textAreaIngredients.setText("");
		horizontalPanel_3.add(textAreaIngredients);
		textAreaIngredients.setSize("400px", "300px");

		HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		horizontalPanel_6.add(horizontalPanel_4);
		horizontalPanel_4.setSize("500px", "400px");

		Label lblNewLabel_3 = new Label("Description:");
		horizontalPanel_4.add(lblNewLabel_3);

		horizontalPanel_4.add(textAreaDescription);
		textAreaDescription.setSize("400px", "300px");

		textAreaDescription.setText(recipe.getDescription());
		textAreaIngredients.setText(recipe.getIngredients());
		textBoxKeywords.setText(recipe.getKeywords());
		textBoxNoPersons.setText(recipe.getNoPersons());
		textBoxTitle.setText(recipe.getTitle());
		initWidget(decoratorPanel);

	}

	public Recipe getRecipe() {
		return recipe;
	}

	public void setRecipe(Recipe recipe) {
		this.recipe = recipe;
	}

	private void showAreYouSure() {
		HTML message;
		dialogbox = new DialogBox(false);
		VerticalPanel vp = new VerticalPanel();
		HorizontalPanel hp = new HorizontalPanel();
		dialogbox.setText("DialogBox");
		message = new HTML("Are you sure?");
		Button btnOk = new Button("Ok");
		Button btnCancel = new Button("Cancel");
		btnOk.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(new Label("Ok"));
				dialogbox.hide();
				deleteRecipe();
			}

		});
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(new Label("Cancel"));
				dialogbox.hide();
			}

		});
		vp.add(message);
		vp.add(btnOk);
		vp.add(btnCancel);
		dialogbox.setWidget(vp);
		dialogbox.center();
	}

	private void deleteRecipe() {
		asyncService.deleteRecipe(recipe, new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(new Label("Recipe deleted"));
				Iterator<Widget> iterator = RootPanel.get("merecipeContainer")
						.iterator();
				Widget widget;
				while (iterator.hasNext()) {
					widget = iterator.next();
					if (widget instanceof RecipesComposite) {
						((RecipesComposite) widget).loadRecipes();
					}
					if (widget instanceof RecipeComposite) {
						RootPanel.get("merecipeContainer").remove(widget);
					}
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(
						new Label(caught.getMessage()));
			}
		});
	}

	private void saveRecipe() {
		recipe.setDescription(textAreaDescription.getText());
		recipe.setIngredients(textAreaIngredients.getText());
		recipe.setKeywords(textBoxKeywords.getText());
		recipe.setNoPersons(textBoxNoPersons.getText());
		recipe.setTitle(textBoxTitle.getText());
		asyncService.saveRecipe(recipe, new AsyncCallback<Recipe>() {

			@Override
			public void onSuccess(Recipe resultRecipe) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(
						new Label("Recipe saved (id:" + resultRecipe.getId()
								+ ")"));
				recipe = resultRecipe;
				Iterator<Widget> iterator = RootPanel.get("merecipeContainer")
						.iterator();
				Widget widget;
				while (iterator.hasNext()) {
					widget = iterator.next();
					if (widget instanceof RecipesComposite) {
						((RecipesComposite) widget).loadRecipes();
					}
					if (widget instanceof RecipeComposite) {
						RootPanel.get("merecipeContainer").remove(widget);
					}
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				RootPanel.get("infoContainer").clear();
				RootPanel.get("infoContainer").add(
						new Label(caught.getMessage()));
			}
		});

	}
}
